﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DevComponents.DotNetBar.Controls;
using System.Data;

namespace BUL
{
    public partial class Business
    {      
        public void GetAllDVT(string strTimkiem, DataGridViewX dataGridViewX )
        {
            if(dataGridViewX.RowCount>0)
                dataGridViewX.Rows.Clear();
            using (var dal = new QuanLyBanHangEntities1())
            {
                var listResult = dal.tblDVTs.Where(x => x.TenDVT.Contains(strTimkiem)).OrderBy(x => x.TenDVT);
                int i = 0;
                foreach (var item in listResult)
                {
                    dataGridViewX.Rows.Add();
                    dataGridViewX.Rows[i].Cells[0].Value = "N";
                    dataGridViewX.Rows[i].Cells[1].Value = item.MaDVT;
                    dataGridViewX.Rows[i].Cells[2].Value = item.TenDVT;
                    dataGridViewX.Rows[i].Cells[3].Value = item.GhiChu;
                    i++;
                }
            }
        }

        public tblDVT GetDVTById(string madvt)
        {
            return _dal.tblDVTs.Where(x => x.MaDVT == madvt).FirstOrDefault();
        }

        public void InsertDVT(tblDVT dvt)
        {
            _dal.tblDVTs.AddObject(dvt);
            _dal.SaveChanges();
        }

        public void UpdateDVT(string maDVTCu, tblDVT dvt)
        {
            var DVTUpdate = _dal.tblDVTs.Where(x => x.MaDVT == maDVTCu).FirstOrDefault();
            DVTUpdate.MaDVT = dvt.MaDVT;
            DVTUpdate.TenDVT = dvt.TenDVT;
            DVTUpdate.GhiChu = dvt.GhiChu;
            _dal.SaveChanges();
        }

        public void DeleteDVT(IEnumerable<string> madvts)
        {
            var listDelete = _dal.tblDVTs.Where(x => madvts.Contains(x.MaDVT));
            foreach (var tbldvt in listDelete)
            {
                _dal.tblDVTs.DeleteObject(tbldvt);
            }
            _dal.SaveChanges();
        }

        public tblDVT BuildDVT(string maDVT, string tenDVT, string ghichu)
        {
            return new tblDVT { MaDVT = maDVT, TenDVT = tenDVT,GhiChu = ghichu };
        }
    }
}
